package kill;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/16 14:15
 * @description:
 */
public class Kill {
private JedisPool jedisPool;
private static final String KILL="PRODUCT_KILL";
@Before
  public void testJedisConfig()
{
  JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
  jedisPoolConfig.setMaxTotal(20);
  jedisPoolConfig.setMinIdle(10);
  jedisPoolConfig.setBlockWhenExhausted(true);
  this.jedisPool=new JedisPool("localhost");
  try(Jedis jedis=this.jedisPool.getResource()){
    jedis.set(KILL,"200");
  }
}
@Test
  public void KillTest()
{
  for (int i=0;i<10000;i++) {
    int index = i;
    Executors.newSingleThreadExecutor().submit(() -> {
          try (Jedis jedis = this.jedisPool.getResource()) {
            Long decr = jedis.decr(KILL);
            if (decr >= 0) { System.out.println("第"+(index+1)+"顾客购买了第"+(200-index)+"个商品"); }
            else {
              System.out.println("被抢完了");
            }
          }
        }
    );
  }
}
}
